package com.color.admin.common.core.utils.web;

import cn.hutool.extra.servlet.ServletUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author HaiQiu
 * @date 2022/1/28
 * 工具
 */
public class ToolUtils {


    /**
     * 获取客户端IP
     * @return IP
     */
    public static String getIp(){
        String clientIP = ServletUtil.getClientIP(ServletUtils.getRequest());
        return clientIP;
    }

    /**
     * 获取浏览器名称
     * @return 浏览器
     */
    public static String getBrowser(){
        String userAgent = ServletUtils.getRequest().getHeader("USER-AGENT");
        UserAgent ua = UserAgent.parseUserAgentString(userAgent);
        Browser browser = ua.getBrowser();
        return browser.getName() + "-" + browser.getVersion(userAgent);
    }

    /**
     * 获取操作系统
     * @return 操作系统
     */
    public static String osName(){
        String userAgent = ServletUtils.getRequest().getHeader("User-Agent");
        UserAgent ua = UserAgent.parseUserAgentString(userAgent);
        OperatingSystem os = ua.getOperatingSystem();
        return os.getName();
    }

    /**
     * 获取IP地址
     * @return
     */
    public static String getIpAddress() {
        HttpServletRequest request = ServletUtils.getRequest();
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_CLUSTER_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_FORWARDED");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_VIA");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("REMOTE_ADDR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip.contains(",")) {
            return ip.split(",")[0];
        } else {
            return ip;
        }
    }


    /**
     * 获取浏览器名称（获取不到IE的信息）
     * @return
     */
    public static String getBrowserName() {
        HttpServletRequest request = ServletUtils.getRequest();
        String browserName = "";
        String userAgent = request.getHeader("User-Agent").toUpperCase();
        if (userAgent == null || userAgent.equals("")) {
            return "";
        }
        if (userAgent.indexOf("MSIE") > 0) {
            browserName = "IE";
        } else if (userAgent.indexOf("FIREFOX") > 0) {
            browserName = "Firefox";
        } else if (userAgent.indexOf("CHROME") > 0) {
            browserName = "Chrome";
        } else if (userAgent.indexOf("SAFARI") > 0) {
            browserName = "Safari";
        } else if (userAgent.indexOf("CAMINO") > 0) {
            browserName = "Camino";
        } else if (userAgent.indexOf("KONQUEROR") > 0) {
            browserName = "Konqueror";
        } else if (userAgent.indexOf("EDGE") > 0) {
            browserName = "Microsoft Edge";
        }
        return browserName;
    }

    /**
     * 获取操作系统名称
     * @return
     */
    public static String getOSname() {
        HttpServletRequest request = ServletUtils.getRequest();
        String osName = "";
        String userAgent = request.getHeader("User-Agent").toUpperCase();
        if (userAgent.contains("WINDOWS")) {
            if (userAgent.contains("WINDOWS NT 10.0")) {
                osName = "Windows 10";
            } else if (userAgent.contains("WINDOWS NT 6.2")) {
                osName = "Windows 8";
            } else if (userAgent.contains("WINDOWS NT 6.1")) {
                osName = "Windows 7";
            } else if (userAgent.contains("WINDOWS NT 6.0")) {
                osName = "Windows Vista";
            } else if (userAgent.contains("WINDOWS NT 5.2")) {
                osName = "Windows XP";
            } else if (userAgent.contains("WINDOWS NT 5.1")) {
                osName = "Windows XP";
            } else if (userAgent.contains("WINDOWS NT 5.01")) {
                osName = "Windows 2000";
            } else if (userAgent.contains("WINDOWS NT 5.0")) {
                osName = "Windows 2000";
            } else if (userAgent.contains("WINDOWS NT 4.0")) {
                osName = "Windows NT 4.0";
            } else if (userAgent.contains("WINDOWS 98; WIN 9X 4.90")) {
                osName = "Windows ME";
            } else if (userAgent.contains("WINDOWS 98")) {
                osName = "Windows 98";
            } else if (userAgent.contains("WINDOWS 95")) {
                osName = "Windows 95";
            } else if (userAgent.contains("WINDOWS CE")) {
                osName = "Windows CE";
            }
        } else if (userAgent.contains("MAC")){
            osName = "Mac";
        } else if (userAgent.contains("UNIX")){
            osName = "UNIX";
        } else if (userAgent.contains("LINUX")){
            osName = "Linux";
        } else if (userAgent.contains("SUNOS")){
            osName = "SunOS";
        }
        return osName;
    }


    /**
     * 根据Ip获取地理位置
     * @return 地理位置
     */
    public static String getLocation(){
        String ip = getIp();
        return getLocation(ip);
    }

    /**
     * 根据Ip地址获取地理位置
     * @param ip IP
     * @return 地理位置
     */
    public static String getLocation(String ip){
        String encoding = "UTF-8";
        String taoBaoIp = "https://ip.taobao.com/outGetIpInfo";
        String address = null;
        Map<String,String> params = new HashMap<>(2);
        params.put("ip", ip);
        params.put("accessKey", "alibaba-inc");
        String result = HttpUtil.request(taoBaoIp, "POST", encoding, null, params, 5000, 5000);
        JSONObject jsonObject = com.alibaba.fastjson.JSON.parseObject(result);
        if (Integer.parseInt(jsonObject.getString("code"))==0){
            String data = jsonObject.getString("data");
            address = JSON.parseObject(data).getString("country") +
                    "-" + JSON.parseObject(data).getString("region") +
                    "-" + JSON.parseObject(data).getString("city") +
                    " " + JSON.parseObject(data).getString("isp");
        }
        return address;
    }

}
